package cn.edu.nju.gitlab.activity;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.edu.nju.gitlab.R;
import cn.edu.nju.gitlab.fragment.ScoreFragment;
import cn.edu.nju.gitlab.injector.DaggerActivityComponent;
import cn.edu.nju.gitlab.injector.MainModule;
import cn.edu.nju.gitlab.model.Exam;
import cn.edu.nju.gitlab.model.Question;
import cn.edu.nju.gitlab.presenter.ScorePresenter;

/**
 * Created by lpt on 2017/6/12.
 */

public class HomeworkActivity extends AppCompatActivity {
    private Exam exam;
    private List<Map<String, Object>> questionMap;
    private List<Question> questionList;

    @BindView(R.id.question_list)
    ListView listView;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.homework_id)
    TextView homeId;
    @BindView(R.id.description)
    TextView description;
    @BindView(R.id.start)
    TextView start;
    @BindView(R.id.end)
    TextView end;
    @BindView(R.id.status)
    TextView status;
    @BindView(R.id.check_analysis)
    Button button;



    @BindView(R.id.data11)
    TextView data11;
    @BindView(R.id.data12)
    TextView data12;
    @BindView(R.id.data13)
    TextView data13;
    @BindView(R.id.data14)
    TextView data14;
    @BindView(R.id.data15)
    TextView data15;
    @BindView(R.id.data16)
    TextView data16;


    public double[] datas;


    @Inject
    ScorePresenter presenter;


    private String username;
    private String password;
    private int assignmentId;
    private int userId;
    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = this.getIntent();
        username = intent.getStringExtra("username");
        password = intent.getStringExtra("password");
        userId = intent.getIntExtra("userId", 0);
        type = intent.getStringExtra("type");
        exam = (Exam) intent.getSerializableExtra("exam");
        assignmentId = exam.getId();
        questionList = exam.getQuestions();
        setContentView(R.layout.homework_layout);
        ButterKnife.bind(this);
        DaggerActivityComponent.builder()
                .mainModule(new MainModule(this))
                .build()
                .inject(this);



        presenter.getLineChartData(username,password,assignmentId);
        try {
            Thread.sleep(Long.parseLong("1000"));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        title.setText(exam.getTitle());
        homeId.setText("" + exam.getId());
        description.setText("" + exam.getDescription());
        start.setText("" + exam.getStartAt());
        end.setText("" + exam.getEndAt());
        status.setText("" + exam.getStatus());
        checkAnalysis();


        showList();

    }

    private void showList() {
        questionMap = new ArrayList<>();
        List<Question> list = exam.getQuestions();
        for (Question question : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("title", "名称：" + question.getTitle());
            map.put("content", "描述：" + question.getDescription());
            questionMap.add(map);
        }
        String[] from = {"title", "content"};
        int[] to = {R.id.title, R.id.content};
        SimpleAdapter adapter = new SimpleAdapter(this, questionMap, R.layout.short_student_info, from, to);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(view.getContext(), QuestionActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("question", questionList.get(position));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }


//
//    private void setLineChart() {
//
//        ScoreFragment fragment = new ScoreFragment();
//        fragment.setUserInfo(username, password, assignmentId);
//        fragment.getData();
//       // TextView view=(TextView)findViewById(R.id.data1);
//
//
//
//    }

    private void checkAnalysis() {
        if (!type.equals("student")) {
            button.setVisibility(View.GONE);
            return;
        }
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), AnalysisActivity.class);
                intent.putExtra("username", username);
                intent.putExtra("password", password);
                intent.putExtra("assignmentId", assignmentId);
                intent.putExtra("userId", userId);
                System.out.println("test1");
                startActivity(intent);
            }
        });
    }
    public void showError(){

    }

    public void showLineChart(double[] data) {
        datas=data;
        System.out.println(data[0]);
        data11.setText(""+(int)data[0]);
        data12.setText(""+(int)data[1]);
        data13.setText(""+(int)data[2]);
        data14.setText(""+(int)data[3]);
        data15.setText(""+(int)data[4]);
        DecimalFormat df = new DecimalFormat("0.00");

        data16.setText(""+df.format(data[5]));

    }
}
